﻿using System;
using System.Windows.Forms;

namespace YArchitech.Controls
{
	public class HYGridViewNumbericColumn : DataGridViewColumn
	{
		public HYGridViewNumbericColumn() : base(new HYGridViewNumbericCell())
		{
			this.m_IsInt = false;
			this.m_IsPositive = true;
			this.DefaultCellStyle = new DataGridViewCellStyle
			{
				Format = "G4",
				NullValue = "0"
			};
		}

		private static DataGridViewTriState TriBool(bool value)
		{
			if (!value)
			{
				return DataGridViewTriState.False;
			}
			return DataGridViewTriState.True;
		}

		public override DataGridViewCell CellTemplate
		{
			get
			{
				return base.CellTemplate;
			}
			set
			{
				if (value != null && !value.GetType().IsAssignableFrom(typeof(HYGridViewNumbericCell)))
				{
					throw new InvalidCastException("类型不属于HYGridViewNumbericCell");
				}
				base.CellTemplate = value;
			}
		}

		public override object Clone()
		{
			HYGridViewNumbericColumn hygridViewNumbericColumn = (HYGridViewNumbericColumn)base.Clone();
			hygridViewNumbericColumn.IsInt = this.IsInt;
			hygridViewNumbericColumn.IsPositive = this.IsPositive;
			return hygridViewNumbericColumn;
		}

		public bool IsInt
		{
			get
			{
				return this.m_IsInt;
			}
			set
			{
				if (this.m_IsInt != value)
				{
					this.m_IsInt = value;
					((HYGridViewNumbericCell)this.CellTemplate).IsInt = value;
					if (base.DataGridView != null && base.DataGridView.Rows != null)
					{
						int count = base.DataGridView.Rows.Count;
						for (int i = 0; i < count; i++)
						{
							DataGridViewCell dataGridViewCell = base.DataGridView.Rows.SharedRow(i).Cells[i];
							if (dataGridViewCell is HYGridViewNumbericCell)
							{
								((HYGridViewNumbericCell)dataGridViewCell).IsInt = value;
							}
						}
					}
				}
			}
		}

		public virtual bool IsPositive
		{
			get
			{
				return this.m_IsPositive;
			}
			set
			{
				if (this.m_IsPositive != value)
				{
					this.m_IsPositive = value;
					((HYGridViewNumbericCell)this.CellTemplate).IsPositive = value;
					if (base.DataGridView != null && base.DataGridView.Rows != null)
					{
						int count = base.DataGridView.Rows.Count;
						for (int i = 0; i < count; i++)
						{
							DataGridViewCell dataGridViewCell = base.DataGridView.Rows.SharedRow(i).Cells[i];
							if (dataGridViewCell is HYGridViewNumbericCell)
							{
								((HYGridViewNumbericCell)dataGridViewCell).IsPositive = value;
							}
						}
					}
				}
			}
		}

		public MostValue ValueRange
		{
			get
			{
				return this.m_MostValue;
			}
			set
			{
				if (this.m_MostValue != value)
				{
					this.m_MostValue = value;
					((HYGridViewNumbericCell)this.CellTemplate).ValueRange = value;
					if (base.DataGridView != null && base.DataGridView.Rows != null)
					{
						int count = base.DataGridView.Rows.Count;
						for (int i = 0; i < count; i++)
						{
							DataGridViewCell dataGridViewCell = base.DataGridView.Rows.SharedRow(i).Cells[i];
							if (dataGridViewCell is HYGridViewNumbericCell)
							{
								((HYGridViewNumbericCell)dataGridViewCell).ValueRange = value;
							}
						}
					}
				}
			}
		}

		private bool m_IsInt;

		private bool m_IsPositive;

		private MostValue m_MostValue = new MostValue(0.0, 0.0);
	}
}
